package com.example.a2032.Utils;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.example.a2032.activity.ClockNotifyActivity;
import com.example.a2032.entity.JstyleClock;

import java.util.Calendar;

public class ClockUtils {
    static final String JSTYLE_ALARMACTION = "android.intent.action.ALARM_RECEIVER";
    static final String JSTYLE_HeartPackageACTION = "android.intent.action.HeartPackage_RECEIVER";
    static final String JSTYLE_ALARMCategory = "android.intent.category.jstyleWatch";
    public static final String JSTYLE_CLOCK = "JSTYLE_CLOCK";
    private static final String TAG = "ClockUtils";
    static ClockUtils clockUtils = new ClockUtils();

    public static ClockUtils getInstance() {
        return clockUtils;
    }

    private ClockUtils() {
    }

    public void setClock(Context context, JstyleClock jstyleClock) {
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Service.ALARM_SERVICE);
        int clockType = jstyleClock.getClockType();
        int id = (int) jstyleClock.getId();
        int startHour = jstyleClock.getHour();
        int startMinute = jstyleClock.getMinute();
        Calendar calendar = Calendar.getInstance();
        int currentHour = calendar.get(Calendar.HOUR_OF_DAY);
        int currentMinute = calendar.get(Calendar.MINUTE);
        //设置的时间如果在当前时间之前，直接设置会马上触发闹钟
        //需要设置到第二天
        if (startHour < currentHour || startMinute < currentMinute) {
            setNextClock(context, jstyleClock);
            return;
        }

        calendar.set(Calendar.HOUR_OF_DAY, startHour);
        calendar.set(Calendar.MINUTE, startMinute);
        calendar.set(Calendar.SECOND, 0);
        Intent intent = new Intent(JSTYLE_ALARMACTION);
        intent.addCategory(JSTYLE_ALARMCategory);
        intent.putExtra(ClockNotifyActivity.KEY_ClockType, clockType);
        intent.putExtra(JSTYLE_CLOCK, jstyleClock);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, id, intent,
                PendingIntent.FLAG_CANCEL_CURRENT);
        alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
    }


    public void setNextClock(Context context, JstyleClock jstyleClock) {
        Log.i(TAG, "setNextClock: ");
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Service.ALARM_SERVICE);
        int clockType = jstyleClock.getClockType();
        int id = (int) jstyleClock.getId();
        int startHour = jstyleClock.getHour();
        int startMinute = jstyleClock.getMinute();
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_YEAR, 1);
        calendar.set(Calendar.HOUR_OF_DAY, startHour);
        calendar.set(Calendar.MINUTE, startMinute);
        calendar.set(Calendar.SECOND, 0);
        Intent intent = new Intent(JSTYLE_ALARMACTION);
        intent.addCategory(JSTYLE_ALARMCategory);
        intent.putExtra(ClockNotifyActivity.KEY_ClockType, clockType);
        intent.putExtra(JSTYLE_CLOCK, jstyleClock);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, id, intent,
                PendingIntent.FLAG_CANCEL_CURRENT);
        alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
    }

    public void setNextHeartPackageClock(Context context) {
        Log.i(TAG, "setNextClock: ");
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Service.ALARM_SERVICE);
        Intent intent = new Intent(JSTYLE_HeartPackageACTION);
        intent.addCategory(JSTYLE_ALARMCategory);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 999, intent,
                PendingIntent.FLAG_CANCEL_CURRENT);
        alarmManager.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 8*60 * 1000, pendingIntent);
    }

    public void setHeartPackageClock(Context context) {
        Log.i(TAG, "setNextClock: ");
        Intent intent = new Intent(JSTYLE_HeartPackageACTION);
        intent.addCategory(JSTYLE_ALARMCategory);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 999, intent,
                PendingIntent.FLAG_CANCEL_CURRENT);
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Service.ALARM_SERVICE);
       // alarmManager.cancel(pendingIntent);

        alarmManager.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 5 * 1000, pendingIntent);
    }
    public void cancelClock(Context context, long id) {
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Service.ALARM_SERVICE);
        Intent intent = new Intent(JSTYLE_ALARMACTION);
        intent.addCategory(JSTYLE_ALARMCategory);
        alarmManager.cancel(PendingIntent.getBroadcast(context, (int) id, intent, PendingIntent.FLAG_UPDATE_CURRENT));

    }
}
